import { ref, computed } from 'vue';
import { defineStore } from 'pinia';
import { getStore, setStore } from '@/utils/utils';

export const usePdfStore = defineStore('pdfStore', () => {
    const pdfList = ref(getStore('esign-pdfList') || []);
    const scale = ref(getStore('esign-pdfList') || 1.5);
    const pdfViewRect = ref(null);

    const addPdfList = item => {
        const flag = pdfList.value.some(p => p.id === item.id);
        if (flag) return;
        pdfList.value.push(item);
        setStore('esign-pdfList', pdfList.value);
    };

    const removePdfList = id => {
        const idx = pdfList.value.findIndex(item => item.id === id);
        if (idx === -1) return;
        pdfList.value.splice(idx, 1);
        setStore('esign-pdfList', pdfList.value);
    };

    const updatePdfList = () => {
        setStore('esign-pdfList', pdfList.value);
    };

    const setScale = val => {
        scale.value = val;
        setStore('esign-scale', val);
    };

    const setPdfViewRect = val => {
        pdfViewRect.value = val;
    };

    return { pdfList, addPdfList, removePdfList, updatePdfList, scale, setScale, pdfViewRect, setPdfViewRect };
});
